// 封装 axios
import axios from 'axios';

// 创建  axios 对象
const request = axios.create({
    baseURL: 'https://api.shop.eduwork.cn/api/',
    timeout: 5000,
    headers: {
        'Content-Type': 'application/json',
    },
});

// 定义 axios 请求拦截器，请求发送之前触发
request.interceptors.request.use(
    (config) => {
        // 获取token
        const token = localStorage.getItem('token');
        if (token) {
            config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
    },
    (error) => {
        return Promise.reject(error);
    },
);

// 定义 axios 响应拦截器，请求发送之后触发
request.interceptors.response.use(
    (response) => {
        return response;
    },
    (error) => {
        return Promise.reject(error);
    },
);

export default request;